from flask import Blueprint,jsonify,g
from flask_restful import Api,Resource,marshal,reqparse
from utils.login_utils import login_required
from models.users import *
from models import db
import json
from models_fields.user_f import news_fields
import datetime


news_bp = Blueprint('news_bp',__name__,url_prefix='/news')
api = Api(news_bp)



class NewResource(Resource):
    # @login_required
    def post(self):
        pargs = reqparse.RequestParser()
        pargs.add_argument('channel_id')
        pargs.add_argument('title')
        pargs.add_argument('cover')
        pargs.add_argument('content')
        args = pargs.parse_args()
        channel_id = args.get('channel_id')
        title = args.get('title')
        cover = args.get('cover')
        content = args.get('content')
        if not all([channel_id, title, content]):
            return jsonify(msg='信息不完整',code=201)

        channel = Channel.query.filter_by(cid=channel_id).first()
        if not channel:
            return jsonify(msg='频道不存在',code=201)
        news = News(
            user_id = 1,
            channel_id = channel_id,
            title = title,
            cover = cover,
            content = content,
        )
        db.session.add(news)
        db.session.commit()
        return jsonify(msg='添加成功',code=200)
    def get(self):
        req = reqparse.RequestParser()
        req.add_argument('user_id')
        args = req.parse_args()
        # 获取用户ID
        user_id = args['user_id']
        # 查找指定用户的资讯信息
        news = News.query.filter_by(user_id=user_id).all()
        data = marshal(news, news_fields)
        # 处理结果数据
        return jsonify(msg='获取成功',code=200,data=data)

class New(Resource):
    def put(self):
        pargs = reqparse.RequestParser()
        pargs.add_argument('nid')
        pargs.add_argument('user_id')
        pargs.add_argument("channel_id")
        pargs.add_argument('title')
        pargs.add_argument('cover')
        pargs.add_argument('content')
        args = pargs.parse_args()
        nid = args.get("nid")
        user_id = args.get("user_id")
        channel_id = args.get('channel_id')
        title = args.get('title')
        cover = args.get('cover')
        content = args.get('content')

        if not all(["nid", "user_id", "channel_id", "title", "cover", "content"]):
            return jsonify(message="文章信息不完整！", code=204)
        news = News.query.filter_by(nid=nid)

        if news:
            news.update({"user_id":user_id, "channel_id":channel_id, "title":title, "cover":cover, "content":content})
            db.session.commit()
            return jsonify(msg='修改成功',code=200)
        else:
            return jsonify(msg='文章不存在',code=204)


class DeleteNew(Resource):
    # @login_required
    def delete(self,nid):
        # news = News.query.filter_by(nid=nid).first()
        # if not news:
        #     return jsonify(msg='文章不存在',code=204)
        # if news.status == News.STATUS.DELETED:
        #     return jsonify(msg='文章已删除',code=200)
        # else:
        #     news.update({'status':4,'delete_time':datetime.now})
        #     return jsonify(msg='删除成功',code=200)
        # apipost测试没法登录
        uid = 1
        news=News.query.filter_by(user_id=uid,nid=nid).first()
        if not news:
            return jsonify(msg='没有该文章',code=204)
        News.query.filter_by(nid=nid).delete()
        db.session.commit()
        return jsonify(msg="删除成功",code=200)




api.add_resource(NewResource,'/add')
api.add_resource(New,'/put')
api.add_resource(DeleteNew,'/del/<int:nid>')


